DataQuery XML attachment
The dataQuery attachment allows values to be queried from a datasource and stored in identifiers. Currently only SQL server queries and CSV file lookups are supported.
The dataQuery attachment is designed to retrieve a single ‘row’ from a data source. It cannot be used to retrieve data sets, although more than one value from the ‘row’ can be retrieved. When querying a SQL database, only existing stored procedures can be executed. It is not possible to execute SQL text directly.
When querying a CSV file, the FIRST matching entry is returned. CSV file queries should only be used for small files e.g. up to a few thousand lines. Searching is not indexed and becomes inefficient for large files.
Schema
<mkzAttachment type="dataQuery">
<onPreSubmit|onSubmit>
<!-- query a SQL database -->
<sqlConnection>connection-string</sqlConnection>
<sqlCommand>stored-procedure-name
<sqlParameter identifier="name" [direction="out"] [type="int"]>param-name</sqlParameter>
<!-- other parameters -->
<sqlColumn identifier="name" [type="int"]>column-name</sqlColumn>
<!-- other columns -->
</sqlCommand>
<!-- query a CSV file -->
<csvFilename>CSV-file-full-path-name</csvFilename>
<csvCommand csvColumn="name-or-index"
equals="text" | contains="text" | equalsIdentifier="name" | containsIdentifier="name" >
<csvColumn identifier="name" >column-name-or-index</csvColumn>
<!-- other columns -->
</csvCommand>
</onPreSubmit|onSubmit>
</mkzAttachment>
The XML can only be executed during the OnPreSubmit and OnSubmit phases. The schema is identical for both phases.
The XML supports the following elements during OnPreSubmit/OnSubmit. There can be multiple instances of any of these.
<sqlConnection>connection-string</sqlConnection> Defines the database connection string.
Defines the database connection string that will be used to connect to the database. The format of this string will depend on the database being queried.
<sqlCommand>stored-procedure-name</sqlCommand> Defines the SQL command to be executed.
This element defines the name of the Stored Procedure to execute. It also wraps the list of parameters and/or columns that will be passed to/from the command.
<sqlParameter identifier="name" >parameter-name<sqlParameter> Defines the SQL parameters.
This element defines (one of) the parameters to the stored procedure. There may be multiple instances of this element, one for each parameter. Each sqlParameter element must specify an identifier. The value contained in the identifier will be passed to the stored procedure as the parameter value. For output parameters, the identifier will, in addition, receive the value contained in the parameter after the stored procedure executes. An ‘@’ character will be pre-pended to the parameter name if one does not already exist.
<sqlColumn identifier="name" >column-name<sqlColumn> Defines the SQL parameters.
This element defines (one of) the columns that the stored procedure will return in its (single) row. There may be multiple instances of this element, one for each column. Each sqlColumn element must specify an identifier that will receive the value contained in the column after the stored procedure executes.
<csvFilename>CSV-filename</csvFilename>
Defines the fully-qualified name of the CSV file.
<csvCommand csvColumn="name-or-index" equals|contains|equalsIdentifier|containsIdentifier="name"/>
This element defines the column that will be tested for a match and the text to match against. It also wraps the list of columns that will receive the matched data. csvColumn can be an integer greater than zero (columns are numbered from 1 to n), or the title of a column as specified in the first row. The string to match against the row is specified directly by equals or indirectly by equalsIdentifier. It can optionally support wildcards by specifying contains or containsIdentifier.
N.B. If the column is specified as an integer then the whole CSV file is searched starting at line 1.
If the column is specified by name, then line 1 is assumed to be a header row and searching starts from line 2.
<csvColumn identifier="name" >column-name-or-index<sqlColumn>
This element defines (one of) the columns that the search will return from its (single) match. There may be multiple instances of this element, one for each column. Each csvColumn element must specify an identifier that will receive the value contained in the column after the stored procedure executes. The column can be specified by integer (1 to n), or by name.
Examples
1. A SQL stored procedure with input and output parameters
CREATE PROCEDURE [dbo].[GetContactDetails]
@username varchar(50),
@email varchar(100) output,
@mobile varchar(16) output
AS
BEGIN
SELECT @email=Email, @mobile=Mobile
FROM Users
WHERE UserName=@username
END
The corresponding XML would be as below. Note how one identifier is defined for and associated with each SQL parameter, both input and output.
<mkzAttachment type="dataQuery">
<onSubmit>
<!-- define an identifier for each parameter of the stored procedure -->
<setIdentifier name="patientName" scan="X,Y,W,H" extract="MRN:" />
<setIdentifier name="patientEmail">empty</setIdentifier>
<setIdentifier name="patientMobile">empty</setIdentifier>
<sqlConnection>Data Source=SERVER;Initial Catalog=NAME;IntegratedSecurity=True </sqlConnection>
<!-- specify the procedure and associate each identifier with a parameter -->
<sqlCommand>GetContactDetails
<sqlParameter identifier="patientName">username</sqlParameter>
<sqlParameter identifier="patientEmail" direction="out">email </sqlParameter>
<sqlParameter identifier="patientMobile" direction="out">mobile </sqlParameter>
</sqlCommand>
</onSubmit>
</mkzAttachment>
After the SQL procedure executes, identifiers associated with SQL ‘out’ parameters will have been set to the returned values.
2. A SQL stored procedure with input parameters and output columns
CREATE PROCEDURE [dbo].[GetLetterReturnAddress]
@UniqueID bigint
AS
BEGIN
SELECT o.Name + ',' + o.Address AS 'Address'
FROM Letters l
JOIN Organisation o ON l.OrganisationID=o.OrganisationID
WHERE l.UniqueID = @UniqueID
END
The corresponding XML would be as below. Note how one identifier is defined for and associated with each SQL parameter and each column that will be returned.
<mkzAttachment type="dataQuery">
<onSubmit>
<!-- define an identifier for each parameter of the stored procedure, and for each column to be returned -->
<setIdentifier name="letterID" letterValue="id" />
<setIdentifier name="orgAddress" >unknown</setIdentifier>
<sqlConnection>Data Source=SERVER;Initial Catalog=NAME;Integrated Security=True </sqlConnection>
<!-- specify the procedure and associate each identifier with either a parameter or a column name -->
<sqlCommand>GetLetterReturnAddress
<sqlParameter identifier="letterID">UniqueID</sqlParameter>
<sqlColumn identifier="orgAddress">Address</sqlColumn>
</sqlCommand>
</onSubmit>
</mkzAttachment>
After the SQL procedure executes, identifiers associated with columns will have been set to the column values.
3. Obtaining column values from a CSV file by column name
| alicejones | London | alice@jones.com | 12345 | |
| fredbloggs | Bristol | fred@bloggs.com | 34567 | |
| johnsmith | Glasgow | john@smith.com | 89102 | |
The corresponding XML would be as below. Note how one identifier is defined for and associated with, the required row and each column that will be returned.
<mkzAttachment type="dataQuery">
<onSubmit>
<!-- define an identifier to compare with the required row, and for each column to be returned -->
<!-- In this example we will assume that the scanned user name is "fredbloggs" -->
<setIdentifier name="scannedUser" scan="100,50,20,10" />
<setIdentifier name="email" >null</setIdentifier>
<setIdentifier name="phone" >null</setIdentifier>
<csvFilename>c:\myfiles\users.csv</csvFilename>
<!-- associate each identifier with either the required row or a column name -->
<csvCommand csvColumn="username" equalsIdentifier="scannedUser" >
<csvColumn identifier="email">email</csvColumn>
<csvColumn identifier="phone">tel</csvColumn>
</csvCommand>
</onSubmit>
</mkzAttachment>
When applied to the file above, the above query would result in:
Identifier email = fred@bloggs.com
Identifier phone = 34567